//用户信息的仓库
export default {
  state:{
    //登录状态
    loginStatus:false,
    //token
    token:null,
    //用户信息：头像、昵称…
    userInfo:{},
  },
  mutations:{
    //一进入项目，就执行该方法：检测用户登录(token)状态
    initUser(state){
      let userInfo = JSON.parse( localStorage.getItem('userInfo') )
      if(userInfo){
        state.loginStatus = true
        state.token = userInfo.token
        state.userInfo = userInfo
      }
    },
    login(state,user){
      state.loginStatus = true
      state.token = user.token
      state.userInfo = user
      //持久化存出==》本地存储
      localStorage.setItem('userInfo',JSON.stringify(user))
    },
    //退出登录
    loginOut(state){
      state.loginStatus = false
      state.token = null
      state.userInfo = {}
      localStorage.removeItem('userInfo')
    }
  }
}